Ticker

6/recent/ticker-posts

Python subjective question for competitive exam (4)

Q1.How will you improve the performance of a program in Python?

There are many ways to improve the performance of a Python program. Some of these are as follows: i. Data Structure: We have to select the right data structure for our purpose in a Python program. ii. Standard Library: Wherever possible, we should use methods from standard library. Methods implemented in standard library have much better performance than user implementation. iii. Abstraction: At times, a lot of abstraction and indirection can cause slow performance of a program. We should remove the redundant abstraction in code. iv. Algorithm: Use of right algorithm can make a big difference in a program. We have to find and select the suitable algorithm to solve our problem with high performance.

Q2. What is the difference between a Tuple and List in Python?

In Python, Tuple and List are built-in data structures. Some of the differences between Tuple and List are as follows: I. Syntax: A Tuple is enclosed in parentheses: E.g. myTuple = (10, 20, “apple”); A List is enclosed in brackets: E.g. myList = [10, 20, 30]; II. Mutable: Tuple is an immutable data structure. Whereas, a List is a mutable data structure. III. Size: A Tuple takes much lesser space than a List in Python. IV. Performance: Tuple is faster than a List in Python. So it gives us good performance.



Q3. What are the benefits of using Python?

Python is strong that even Google uses it. Some of the benefits of using Python are as follows: i. Efficient: Python is very efficient in memory management. For a large data set like Big Data, it is much easier to program in Python. ii. Faster: Though Python code is interpreted, still Python has very fast performance. iii. Wide usage: Python is widely used among different organizations for different projects. Due to this wide usage, there are thousands of add-ons available for use with Python. iv. Easy to learn: Python is quite easy to learn. This is the biggest benefit of using Python. Complex tasks can be very easily implemented in Python.

Q4.What is the difference between List and Dictionary data types in Python?

Main differences between List and Dictionary data types in Python are as follows: I. Syntax: In a List we store objects in a sequence. In a Dictionary we store objects in key-value pairs. II. Reference: In List we access objects by index number. It starts from 0 index. In a Dictionary we access objects by key specified at the time of Dictionary creation. III. Ordering: In a List objects are stored in an ordered sequence. In a Dictionary objects are not stored in an ordered sequence. IV. Hashing: In a Dictionary, keys have to be hashable. In a List there is no need for hashing.

Q5.What are the different built-in data types available in Python?

Some of the built-in data types available in Python are as follows: Numeric types: These are the data types used to represent numbers in Python. int: It is used for Integers long: It is used for very large integers of non-limited length. float: It is used for decimal numbers. complex: This one is for representing complex numbers Sequence types: These data types are used to represent sequence of characters or objects. str: This is similar to String in Java. It can represent a sequence of characters. bytes: This is a sequence of integers in the range of 0-255. byte array: like bytes, but mutable (see below); only available in Python 3.x list: This is a sequence of objects. tuple: This is a sequence of immutable objects. Sets: These are unordered collections. set: This is a collection of unique objects. frozen set: This is a collection of unique immutable objects. Mappings: This is similar to a Map in Java. dict: This is also called hashmap. It has key value pair to store information by using hashing



Post a Comment

0 Comments